﻿(function ($) {
    setVal = function ($selecter, myMap, vtext) {
        var others = $selecter.attr("other") || "";
        var ojson = others.length > 0 ? eval("(" + others + ")") : eval([]);
        if (!myMap) return;
        if (ojson.length > 0) {
            for (var i = 0; i < ojson.length; i++) {
                for (var key in ojson[i]) {
                    var obj = myMap.get(key);
                    if (obj)
                        obj.val(ojson[i][key]);
                }
            }
        }
        if (vtext)
            myMap.get(vtext).val($selecter.text());
    };
    SetNull = function (myMap) {
        for (var i = 0; i < myMap.size() ; i++) {
            var obj = myMap.element(i);
            if(obj)
                obj.value.val("");
        }
    };
    $.fn.selectToCols = function (myMap) {
        $this = $(this);
        var myMap = new Map();
        var vtext = null;//select文本值,需指定datatext属性

        var columns = $this.attr("columns") || "";
        var prefix = $this.attr("prefix") || "";
        if (prefix) prefix = prefix + ".";
        var coljson = columns.length > 0 ? eval("(" + columns + ")") : eval([]);
        if (coljson.length > 0) {
            for (var i = 0; i < coljson.length; i++) {
                for (var key in coljson[i]) {
                    var $target = $("<input type='hidden' id='" + prefix + key + "' name='" + prefix + key + "' alt='" + coljson[i][key] + "' value=''/>\r\n");
                    $this.after($target);
                    myMap.put(key, $target);
                }
            }
        }
        var datatext = $this.attr("datatext") || "";
        if (datatext) {//生成select text字段 需要指定datatext属性
            vtext = datatext.substr(0, datatext.indexOf(":"));
            var $target = $("<input type='hidden' id='" + prefix + vtext + "' name='" + prefix + vtext + "' alt='' value=''/>");
            $this.after($target);
            myMap.put(vtext, $target);
        }
        var defSelecter = $this.find("option:selected");
        //var selecterJson = defSelecter.attr("other").length > 0 ? eval("(" + others + ")") : eval([]);
        if ($this.val()) {
            setVal(defSelecter, myMap, vtext);
        }
        else { SetNull(myMap); }
        $this.change(function () {
            $obj = $(this);
            if ($obj.val()) {
                setVal($obj.find("option:selected"), myMap, vtext);
            }
            else { SetNull(myMap); }
        });
    };
    $.fn.checkboxToCols = function () {
        $pbox = $(this);
        var j = 0;
        $(this).find("input[type=checkbox]").each(function () {
            $this = $(this);
            var boxname = $this.attr("id").replace(".", "_") + "spanCheckToMutilCols";
            var $box = $("<span id='" + boxname + "'></span>");
            $("#" + boxname).remove();
            if (this.checked) {
                var columns = $this.attr("columns") || "";
                var prefix = $this.attr("prefix") || "";
                if (prefix) prefix = prefix + "[" + j + "].";
                var coljson = columns.length > 0 ? eval("(" + columns + ")") : eval([]);
                if (coljson.length > 0) {
                    for (var i = 0; i < coljson.length; i++) {
                        for (var key in coljson[i]) {
                            var $target = $("<input type='hidden' id='" + prefix + key + "' name='" + prefix + key + "' value='" + coljson[i][key] + "' />\r\n");
                            $target.appendTo($box);
                        }
                    }
                    j += 1;
                    $this.after($box);
                }
            }
            $this.click(function () { $pbox.checkboxToCols(); });
        });
    };

})(jQuery);